<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="hadd(1,0)">添加权限</el-button>
        </div>
        <el-table row-key="id" :data="list" border>
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button type="text" @click="hadd(2,row.id)">添加</el-button>
              <el-button type="text" @click="hedit(row)">编辑</el-button>
              <el-button type="text" @click="dodel(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <el-dialog :visible.sync="showDialog" title="弹层标题" @close="resetformm">
        <!-- 表单内容 -->
        <el-form ref="refrole" :model="formData" :rules="rules" label-width="100px">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述" prop="description">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="formData.enVisible"
              active-text="启用"
              active-value="1"
              inactive-text="不启用"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>

        <template #footer>
          <div style="text-align: right;">
            <el-button @click="showDialog = false">取消</el-button>
            <el-button type="primary" @click="hsubmit">确定</el-button>
          </div>
        </template>
      </el-dialog>

    </div>
  </div>
</template>
<script>
import { getPermissionList, addPermission, delPermission, updatePermission } from '@/api/permissions'
import { trenDatatoTree } from '@/utils/index'

export default {
  name: 'VueHrPermissions',

  data() {
    const validCode = (rule, value, callback) => {
      let exitcode = this.originList.map(item => item.code)
      if (this.isEdit) {
        exitcode = this.originList.filter(item => item.id !== this.formData.id).map(item => item.code)
      }
      if (exitcode.includes(value)) {
        callback(new Error(`${value}已经存在`))
      } else {
        callback()
      }
    }
    const validname = (rule, value, callback) => {
      let exitname = this.originList.filter(item => item.pid !== this.formData.id).map(item => item.name)
      if (this.isEdit) {
        const pid = this.originList.find(item => item.id === this.formData.pid)
        const brother = this.originList.filter(item => item.pid === pid)
        exitname = brother.filter(item => item.id !== this.formData.id).map(item => item.name)
      }
      if (exitname.includes(value)) {
        callback(new Error(`${value}已经存在`))
      } else {
        callback()
      }
    }
    return {
      pid: '',
      isEdit: false,
      list: [],
      showDialog: false,
      originList: [],
      formData: {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      },
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' },
          { validator: validname, trigger: 'blur' }
        ], // 名称
        code: [{ message: '不能为空', required: true, trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }],
        description: [
          { required: true, message: '请输描述', trigger: 'blur' }
        ] // 描述
      }
    }
  },

  mounted() {

  },
  created() {
    this.loadPermissionList()
  },

  methods: {
    async loadPermissionList() {
      try {
        const res = await getPermissionList()
        console.log('getPermissionList', res)
        this.originList = res.data.map((item) => {
          return { name: item.name, code: item.code, id: item.id, pid: item.pid }
        })
        this.list = trenDatatoTree(res.data)
        // 保存数据
      } catch (err) {
        console.log('getPermissionList', err)
      }
    },
    hadd(type, pid) {
      this.formData.type = type
      this.formData.pid = pid
      this.isEdit = false
      this.showDialog = true
    },
    // 新增权限
    async doadd() {
      try {
        const res = await addPermission(this.formData)
        console.log('addPermission', res)
        this.loadPermissionList()
        // 保存数据
      } catch (err) {
        console.log('addPermission', err)
      }
    },
    hsubmit() {
      this.showDialog = false
      this.isEdit ? this.doedit() : this.doadd()
    },
    // 关闭弹窗清空表单
    resetformm() {
      this.$refs.refrole.resetFields()
      this.formData = {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      }
    },
    // 删除功能
    dodel(id) {
      // 保存数据
      this.$confirm('确定是否删除?').then(async() => {
        const res = await delPermission(id)
        console.log('delPermission', res)
        this.loadPermissionList()
      })
    },
    hedit(data) {
      this.isEdit = true
      this.showDialog = true
      this.formData = { ...data }
    },
    // 更新权限
    async doedit() {
      try {
        const res = await updatePermission(this.formData)
        console.log('updatePermission', res)
        this.loadPermissionList()
        // 保存数据
      } catch (err) {
        console.log('updatePermission', err)
      }
    }
  }
}
</script>
